package cn.singno.springBoot.demo.ch16_mvc;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/anno")
public class DemoAnnoController {

	@RequestMapping(produces="text/plain;charset=UTF-8")
	@ResponseBody
	public String index(HttpServletRequest request){
		return "url:" + request.getRequestURL() + " can access";
	}
	
	@RequestMapping(value="/pathvar/{str}" , produces="text/plain;charset=UTF-8")
	@ResponseBody
	public String pathvar(@PathVariable String str, HttpServletRequest request){
		return "url:" + request.getRequestURL() + " can access,str:" + str;
	}
	
	@RequestMapping(value="/requestParam" , produces="text/plain;charset=UTF-8")
	@ResponseBody
	public String requestParam(Long id, HttpServletRequest request){
		return "url:" + request.getRequestURL() + " can access,id:" + id;
	}
	
	@RequestMapping(value="/obj" , produces="application/json;charset=UTF-8")
	@ResponseBody
	public String obj(DemoObj obj, HttpServletRequest request){
		return "url:" + request.getRequestURL() + " can access,obj id:" + obj.getId() + " obj name:" + obj.getName();
	}
	
	@RequestMapping(value={"/name1", "/name2"} , produces="text/plain;charset=UTF-8")
	@ResponseBody
	public String name(HttpServletRequest request){
		return "url:" + request.getRequestURL() + " can access";
	}
	
	
	@RequestMapping(value="/obj2" , produces="application/json;charset=UTF-8")
	@ResponseBody
	public String obj2(DemoObj obj, HttpServletRequest request){
		DateFormat dateFormat = new SimpleDateFormat("YYYY");
		System.out.println(dateFormat.format(obj.getBirthday()));
		System.out.println(obj.getIsTrue());
		
		return "url:" + request.getRequestURL() + " can access,obj id:" + obj.getId() + " obj name:" + obj.getName();
	}
}
